package com.jinwei.qunguanjia.web.monitor.controller;

import com.jinwei.qunguanjia.common.annotation.Log;
import com.jinwei.qunguanjia.common.core.page.PageDataInfo;
import com.jinwei.qunguanjia.common.enums.BusinessTypeEnum;
import com.jinwei.qunguanjia.common.utils.JsonUtils;
import com.jinwei.qunguanjia.common.utils.PageDataUtil;
import com.jinwei.qunguanjia.common.utils.PageUtils;
import com.jinwei.qunguanjia.common.utils.poi.ExcelUtil;
import com.jinwei.qunguanjia.system.dto.SysLoginLogDTO;
import com.jinwei.qunguanjia.system.service.SysLoginLogService;
import com.jinwei.qunguanjia.web.monitor.vo.SysLoginLogVO;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 系统访问记录
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/monitor/loginLog")
public class SysLoginLogController {
    @Resource
    private SysLoginLogService sysLoginLogService;

    @PreAuthorize("@ss.hasPermission('monitor:logininfor:list')")
    @GetMapping("/list")
    public PageDataInfo list(SysLoginLogVO sysLoginLogVO) throws IllegalAccessException {
        SysLoginLogDTO sysLoginLogDTO = new SysLoginLogDTO();
        BeanUtils.copyProperties(sysLoginLogVO, sysLoginLogDTO);
        PageUtils.startPage();
        PageDataInfo<SysLoginLogDTO> pageDataInfo = sysLoginLogService.listPage(sysLoginLogDTO);
        PageDataUtil<SysLoginLogDTO, SysLoginLogVO> pageDataUtil = new PageDataUtil<>();
        return pageDataUtil.transform(pageDataInfo, SysLoginLogVO.class);
    }

    @Log(title = "登录日志-导出", businessType = BusinessTypeEnum.EXPORT)
    @PreAuthorize("@ss.hasPermission('monitor:logininfor:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLoginLogVO sysLoginLogVO) throws IllegalAccessException {
        SysLoginLogDTO sysLoginLogDTO = new SysLoginLogDTO();
        BeanUtils.copyProperties(sysLoginLogVO, sysLoginLogDTO);
        ExcelUtil<SysLoginLogVO> util = new ExcelUtil<>(SysLoginLogVO.class);
        util.exportExcel(response, JsonUtils.copyList(sysLoginLogService.listPage(sysLoginLogDTO).getData(), SysLoginLogVO.class), "登录日志");
    }
}
